package icu.sehnen.controller;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import icu.sehnen.common.CommonResult;
import icu.sehnen.model.UserModel;
import icu.sehnen.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController

@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    @GetMapping("/login")
    public CommonResult login(String username,String password){

        Integer res = userService.login(username,password);
        if(res!=null){
            return  CommonResult.success("登录成功");
        }
        return  CommonResult.failed("登录失败");
    }

    @PostMapping("/register")
    public CommonResult register(UserModel user){
        Integer res = userService.register(user);
        if(res!=null){
            return  CommonResult.success("注册成功");
        }
        return  CommonResult.failed("注册失败");
    }
    @GetMapping("/list")
    public CommonResult getUserList(String query,Integer pageNum,Integer  pageSize) {
   PageInfo<UserModel> userModelPageInfo=userService.getUsers(pageNum,pageSize,query);
       if(userModelPageInfo!=null){
           return  CommonResult.success(userModelPageInfo);
       }
     return  CommonResult.failed();
    }
    @PutMapping("/switch/{id}/{status}")
    public CommonResult switchState(@PathVariable("id")Long id,@PathVariable("status") Integer status) {
        UserModel userModel=new UserModel();
        userModel.setId(id);
        userModel.setStatus(status);
        Integer res=userService.swichStats(id,status);
        if(res!=null){
            return  CommonResult.success("更新成功");
        }
        return  CommonResult.failed();
    }
    @PostMapping("/add")
    public  CommonResult  addUser(UserModel userModel){
        userService.addUser(userModel);
        return  CommonResult.success();
    }

    @GetMapping("/find/{id}")
    public  CommonResult  findUserById(@PathVariable Long id){
       UserModel userModel=userService.findUserbyId(id);
        return  CommonResult.success(userModel);
    }

    @DeleteMapping("/delete/{id}")
    public  CommonResult deleteUserById(@PathVariable Long id){
            userService.deleteUserbyId(id);
        return  CommonResult.success();
    }

}
